﻿using System;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.Web;
using shop.cart;

namespace comix
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>

    public class removeFromCartAjax : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            StringBuilder sb = new StringBuilder();
            XmlWriter xw = XmlWriter.Create(sb);
            xw.WriteStartElement("result");
            int id = Convert.ToInt32(context.Request.Form["id"]);
            context.Response.ContentType = "text/xml";
            if (id > 0)
            {
                Cart c = (Cart)context.Session["cart"];
                
                bool exists = false;
                
                foreach (CartItem cci in c.Items)
                {
                    
                    if (cci.Item.ItemId() == id)
                    {
                        c.Items.Remove(cci);
                        xw.WriteAttributeString("id", id.ToString());
                        xw.WriteAttributeString("total", c.TotalPrice().ToString());
                        exists = true;
                        break;
                    }
                }
                if (!exists)
                    xw.WriteAttributeString("id", "-1");
              
            }
            xw.WriteEndElement();
            xw.Close();
            context.Response.Write(sb.ToString());
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
